<?php
    require_once($application_folder."/libraries/Merchant.php"); 
    class Service extends CI_Controller 
    {
        var $translate_lang;
        function __construct() { parent::__construct(); }  

        function index() { show_404(); }

        function pay ($booking_id=NULL,$type=NULL,$lang=NULL){
            
            /*echo $booking_id."<br>";
            echo $type."<br>";
            echo $lang."<br>";
            exit;*/
            
            if(!isset($booking_id)){echo 'Please specify booking ID'; exit;}
            if(!isset($type)){echo 'Please specify booking TYPE'; exit;}
            if(!isset($lang)){
                $this->translate_lang = 'me';
            }else $this->translate_lang = $lang; 

            $this->load->model('booking_info');

            switch ($type) {

                case 'rentacar':
                    $row = $this->booking_info->car_book_info($booking_id);        
                    $table = "carbooking";
                    $car = $this->booking_info->car_details_info($row['carid']);
                    $description = $car['name'].'-'.$row['numofdays'].' day/s';

                    break;

                case 'excursion':
                    $row = $this->booking_info->excursion_book_info($booking_id);
                    $table = "excursionbooking";
                    $exc = $this->booking_info->excursion_details_info($row['excursions_id']);
                    $description = $this->getArray($exc['title'],TRUE).'-'.$row['noperson'].' person/s';

                    break;

                case 'tour':
                    
                    $row = $this->booking_info->tour_book_info($booking_id);
                    $table = "tourbooking";
                    $tour = $this->booking_info->tour_details_info($row['tours_id']);
                    $description = $this->getArray($tour['title'],TRUE).'-'.$row['noperson'].' person/s';

                    break;

                case 'avio':
                
                    $table = "airticketbooking";
                    $row = $this->booking_info->avio_book_info($booking_id);
                    $description = $row['description'];

                    break;
            }

            //print_r($row);
            //print_r($exc);

            // ADD 3% takes for each transaction
            $amount = $row['totalprice']*100 + (($row['totalprice']*100)*0.03);
            $currency = 978;
            $client_ip_addr = $_SERVER['REMOTE_ADDR']; 
            $language = "en";
            $cert = "./certificate/test.pem";
            $password = "testpass";
            $merchant = new Merchant("https://secureshop-test.firstdata.lv:8443/ecomm/MerchantHandler", $cert, $password, 1);
            $resp = $merchant -> startDMSAuth($amount, $currency, $client_ip_addr, $description, $language);

            if (substr($resp,0,14)=="TRANSACTION_ID") { 
                $trans_id=urlencode(substr($resp,16,28));
                $url = "https://secureshop-test.firstdata.lv/ecomm/ClientHandler?trans_id=$trans_id";

                // Insert into table transaction
                $sql = mysql_query("INSERT INTO transaction VALUES ('', '$trans_id', ".$amount.",".$currency.",'".$client_ip_addr."' , '".$description."', '', 'NO', '???', '???', '???', '???', now(), '$resp', '', '', '$type')");
                if (!$sql) {
                    die('*** Invalid query 1: ' . mysql_error());
                }

                // Update booking with transaction_id     
                $sql = mysql_query("UPDATE ".$table." SET trans_id='".$trans_id."' WHERE id=".$booking_id);                   

                if (!$sql) {
                    die('*** Invalid query 2: ' . mysql_error());
                }

                header("Location: $url");
            }
            else {
                echo "$resp \n";
            }

        }

        function get_string_between($string, $start, $end){
            $string = " ".$string;
            $ini = strpos($string,$start);
            if ($ini == 0) return "";
            $ini += strlen($start);
            $len = strpos($string,$end,$ini) - $ini;
            return substr($string,$ini,$len);
        }

        function getArray($post_title,$retrive_result=FALSE){

            $titles = array();

            $languages = array('me','en','ru','fr','al'); 

            foreach($languages as $l){

                $str = $this->get_string_between($post_title,'<!--:'.$l.'-->','<!--:-->');

                if($str!=""){
                    $titles[$l] = $str;
                }

            }

            if(count($titles)==0)return "No matches";

            if($retrive_result){
                if(isset($titles[$this->translate_lang])){
                    return $titles[$this->translate_lang];   //String
                } else return "";                       //String

            }else return $titles;                       //Array

        }
}